//
//  Keyboard.m
//  EFExtend
//
//  Created by mac on 15/5/30.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "Keyboard.h"


@interface Keyboard ()

@end

@implementation Keyboard{
    NSMutableArray *boards;
}

- (void)dealloc
{
   
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    boards = [[NSMutableArray alloc] init];
    
    [boards addObject:@{@"title":@"input",@"type":@"a"}];
    [boards addObject:@{@"title":@"input2",@"type":@"a"}];
    [boards addObject:@{@"title":@"input3",@"type":@"a"}];
    [boards addObject:@{@"title":@"input4",@"type":@"a"}];
    [boards addObject:@{@"title":@"input5",@"type":@"a"}];
    [boards addObject:@{@"title":@"input6",@"type":@"a"}];
    [boards addObject:@{@"title":@"input7",@"type":@"a"}];
    [boards addObject:@{@"title":@"input8",@"type":@"a"}];
    [boards addObject:@{@"title":@"input9",@"type":@"a"}];
    [boards addObject:@{@"title":@"input10",@"type":@"a"}];
    [boards addObject:@{@"title":@"input11",@"type":@"a"}];
    [boards addObject:@{@"title":@"input12",@"type":@"a"}];
    [boards addObject:@{@"title":@"input13",@"type":@"a"}];
    [boards addObject:@{@"title":@"input14",@"type":@"a"}];
    [boards addObject:@{@"title":@"input15",@"type":@"a"}];
    [boards addObject:@{@"title":@"input16",@"type":@"a"}];
    [boards addObject:@{@"title":@"input17",@"type":@"a"}];
    [boards addObject:@{@"title":@"input18",@"type":@"a"}];
    [boards addObject:@{@"title":@"input19",@"type":@"a"}];
    
    [self.tableView registerNib:[UINib  nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"LabelInputCell"];
    
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }else{
            [HMUIKeyboard hideKeyboard];
        }
    }
}


#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return boards.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"LabelInputCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    
    
    UIView *view = [cell.contentView viewWithTagString:@"input"];
    if (view==nil) {
        view = [[UIView viewAsInput] EFTagString:@"input"];
        [cell.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(cell.contentView.mas_top).offset(0);
            make.left.mas_equalTo(cell.contentView.mas_left).offset(0);
            make.bottom.mas_equalTo(cell.contentView.mas_bottom).offset(0);
            make.right.mas_equalTo(cell.contentView.mas_right).offset(0);
        }];
    }
    [view EFNextResponder:nil];
    view.tag = indexPath.row;
    [view EFPlaceText:@"test"];
    [view EFText:[[boards objectAtIndex:indexPath.row] valueForKey:@"title"]];
    
    if (boards.count==indexPath.row+1) {
        [view EFAutoHideKeyboardWhenReturn:YES];
    }else{
        [view EFAutoHideKeyboardWhenReturn:NO];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
ON_TextField(signal){
    HMUITextField *field = signal.source;
    if ([signal is:[HMUITextField WILL_ACTIVE]]) {
        [HMUIKeyboard sharedInstance].visabler = field;
        [HMUIKeyboard sharedInstance].accessor = self.tableView;
        UITableViewCell *cellNext = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:field.tag+1 inSection:0]];
        UIView *nxt =[cellNext.contentView viewWithTagString:@"input"];
        [field EFNextResponder:nxt];
    }
}

@end
